package hist.mobilprogramming.tictactoeapplication;


import java.util.Random;

  
/**
 * Class that handle artificial decision. 
 * Only supporting a 3x3 grid.
 * @author godtltro
 */
public class ArtificialOpponent 
{  
	private int[][] _grid;
	
	public ArtificialOpponent(int[][] grid)
	{
		_grid = grid;
	}
	  
     /**
     * @return Position with row id and column id that artificial decided to occupy  
     */
    public SquareElement getElement() 
     {
    	 int row = 0;
    	 int col = 0;
    	 
         if(_grid[0][0]==0 &&  
                 ((_grid[0][1]==1 && _grid[0][2]==1) ||  
                   (_grid[1][1]==1 && _grid[2][2]==1) ||  
                   (_grid[1][0]==1 && _grid[2][0]==1))) {  
             
             row = 0;
        	 col = 0;
             
         } else if (_grid[0][1]==0 &&  
                 ((_grid[1][1]==1 && _grid[2][1]==1) ||  
                   (_grid[0][0]==1 && _grid[0][2]==1))) {  

             row = 0;
        	 col = 1;
         } else if(_grid[0][2]==0 &&  
                 ((_grid[0][0]==1 && _grid[0][1]==1) ||  
                   (_grid[2][0]==1 && _grid[1][1]==1) ||  
                   (_grid[1][2]==1 && _grid[2][2]==1))) {  

               row = 0;
          	   col = 2;
         } else if(_grid[1][0]==0 &&  
                   ((_grid[1][1]==1 && _grid[1][2]==1) ||  
                    (_grid[0][0]==1 && _grid[2][0]==1))){  

               row = 1;
          	   col = 0;
         } else if(_grid[1][1]==0 &&  
                   ((_grid[0][0]==1 && _grid[2][2]==1) ||  
                    (_grid[0][1]==1 && _grid[2][1]==1) ||  
                    (_grid[2][0]==1 && _grid[0][2]==1) ||  
                    (_grid[1][0]==1 && _grid[1][2]==1))) {  
        	 row = 1;
        	 col = 1;  
         } else if(_grid[1][2]==0 &&  
                   ((_grid[1][0]==1 && _grid[1][1]==1) ||  
                    (_grid[0][2]==1 && _grid[2][2]==1))) {  
               
               row = 1;
          	   col = 2;
         } else if(_grid[2][0]==0 &&  
                   ((_grid[0][0]==1 && _grid[1][0]==1) ||  
                    (_grid[2][1]==1 && _grid[2][2]==1) ||  
                    (_grid[1][1]==1 && _grid[0][2]==1))){  
        	 row = 2;
        	 col = 0;  
         } else if(_grid[2][1]==0 &&  
                   ((_grid[0][1]==1 && _grid[1][1]==1) ||  
                    (_grid[2][0]==1 && _grid[2][2]==1))) {  

             row = 2;
          	 col = 1;
         }else if( _grid[2][2]==0 &&  
                   ((_grid[0][0]==1 && _grid[1][1]==1) ||  
                    (_grid[0][2]==1 && _grid[1][2]==1) ||  
                    (_grid[2][0]==1 && _grid[2][1]==1))) {  

             row = 2;
          	 col = 2;
         }  
         // There is nothing to stop so choose a random square  
         else 
         {  
               Random rand = new Random();  
               row = rand.nextInt(3);  
               col = rand.nextInt(3);  
               
               boolean searh = true;
               while(searh && (_grid[row][col]==1 || _grid[row][col]==2)) 
               {  
            	   row = rand.nextInt(3);  
                   col = rand.nextInt(3);
                   
                   int searhCounter = 0;
                   boolean pos00 = false;
                   boolean pos01 = false;
                   boolean pos02 = false;
                   boolean pos10 = false;
                   boolean pos11 = false;
                   boolean pos12 = false;
                   boolean pos20 = false;
                   boolean pos21 = false;
                   boolean pos22 = false;
                   
                   if (row == 0 && col == 0 && pos00 == false)
                   {
                	   searhCounter++;
                	   pos00 = true;
                   }
                   
                   if (row == 0 && col == 1 && pos01 == false)
                   {
                	   searhCounter++;
                	   pos01 = true;
                   }
                   
                   if (row == 0 && col == 2 && pos02 == false)
                   {
                	   searhCounter++;
                	   pos02 = true;
                   }
                   
                   if (row == 1 && col == 0 && pos10 == false)
                   {
                	   searhCounter++;
                	   pos10 = true;
                   }
                   
                   if (row == 1 && col == 1 && pos11 == false)
                   {
                	   searhCounter++;
                	   pos11 = true;
                   }
                   
                   if (row == 1 && col == 2 && pos12 == false)
                   {
                	   searhCounter++;
                	   pos12 = true;
                   }
                   
                   if (row == 2 && col == 0 && pos20 == false)
                   {
                	   searhCounter++;
                	   pos20 = true;
                   }
                   
                   if (row == 2 && col == 1 && pos21 == false)
                   {
                	   searhCounter++;
                	   pos21 = true;
                   }
                   
                   if (row == 2 && col == 2 && pos22 == false)
                   {
                	   searhCounter++;
                	   pos22 = true;
                   }
                   
                   if (searhCounter >= 9)
                	   break;
               }  
               
         }  
         return new SquareElement(row, col);
     }  
       
}   
